/**
 * 对象参数转化为路径参数
 * @param {Object} obj 对象数据
 * @param {String} url 原始路径
 * @param {Boolean} isEncode 是否编译原参数
 */
export function params2string(obj:any, url:string = "", isEncode:boolean = false):string {
	if (!obj || JSON.stringify(obj) === '{}') return url ? url : ""
	url += url ? "?" : ""
	
	if (isEncode) {
		for (let i in obj) {
			url += i + "=" + encodeURIComponent(obj[i]) + "&"
		}
	} else {
		for (let i in obj) {
			url += i + "=" + obj[i] + "&"
		}
	}
	url = url.substr(0, url.length - 1)
	return url
}

/**
 * 将对象中的每一项URI反转译一次
 * @param {Object} obj 输入对象
 */
export function objectDecodeURI(obj:any):any {
	if (typeof obj !== 'object') {
		console.log("invalid params!")
		return {}
	}
	for (let i in obj) {
		if (typeof obj[i] === 'string') {
			obj[i] = decodeURIComponent(obj[i])
		}
	}
	return obj
}


export function getField(obj:any, keyname = "") {
	if (!obj) return false
	let result = null
	for (let item of obj) {
		if (item.fullname === keyname) {
			// console.log("finding!!!", keyname, item.value)
			return item.value
		} else if (item.children && item.children.length > 0) {
			const res = getField(item.children, keyname)
			if (res) result = res
		}
	}
	return result
}

/**
 * 弧度转化
 */
function getRad(d:number) {
	var PI = Math.PI
	return d * PI / 180.0
}

/**
 * 经纬度求距离
 * @param {Number} lng1 坐标1经度
 * @param {Number} lat1 坐标1纬度
 * @param {Number} lng2 坐标2经度
 * @param {Number} lat2 坐标2纬度
 */
export function CoolWPDistance(lng1:number, lat1:number, lng2:number, lat2:number) {
		var f = getRad((lat1 + lat2) / 2)
		var g = getRad((lat1 - lat2) / 2)
		var l = getRad((lng1 - lng2) / 2)
		var sg = Math.sin(g)
		var sl = Math.sin(l)
		var sf = Math.sin(f)
		var s:number, c:number, w:number, r:number, d:number, h1:number, h2:number
		var a = 6378137.0;
    var fl = 1 / 298.257;
    sg = sg * sg
    sl = sl * sl
    sf = sf * sf
    s = sg * (1 - sl) + (1 - sf) * sl
    c = (1 - sg) * (1 - sl) + sf * sl
    w = Math.atan(Math.sqrt(s / c))
    r = Math.sqrt(s * c) / w
    d = 2 * w * a
    h1 = (3 * r  - 1) / 2 / c
    h2 = (3 * r + 1) / 2 / s
    s = d  *  (1 + fl * (h1 * sf * (1 - sg)  -  h2 * (1 - sf) * sg))
		
    return s;
}

/**
 * 手机号敏感信息隐藏 仅限11位
 */
export function phoneHid(phone:string): string {
	if (phone.length === 11) {
		return String(phone).substring(0, 3) + "****" + String(phone).substring(7, 11)
	} else {
		return phone
	}
}

/**
 * URI参数拼接
 */
export function obj2str(obj:any):string {
	let str = ""
	for (let i in obj) {
		str += i + "=" + (obj[i] ? obj[i] : "") + "&"
	}
	return str.substring(0, str.length - 1)
}


/**
 * url临时访问地址转blob对象
 * @param { url, success, fail } 临时地址，成功回调，失败回调
 */
export function createObjectBlob(e: any) {
	const { url } = e;
	try {
		if(url.startsWith('data:')) {
			let arr = url.split(','),
				type = arr[0].match(/:(.*?);/)[1],
				bstr = atob(arr[1]),
				n = bstr.length,
				u8arr = new Uint8Array(n);
			while (n--) u8arr[n] = bstr.charCodeAt(n);
			e.success({
				blob: new Blob([u8arr], { type })
			});
		} else {
			let xhr = new XMLHttpRequest();
			xhr.responseType = 'blob';
			xhr.onload = () => e.success({
				blob: xhr.response
			});
			if (e.fail) xhr.onerror = e.fail;
			xhr.open('GET', url);
			xhr.send();
		}
	} catch(err) {
		if (e.fail) e.fail(err)
	}
}

/**
 * 获取文件后缀名
 * @param {string} filepath 文件路径或者文件名
 * @return {string} 文件后缀
 */ 
export function getSuffix(filepath: string): string {
	const match = filepath.match(/\.([0-9a-z]+)(?:[\?#]|$)/i)
	return match ? match[1] : null
}

/**
 * 判断数组是否有交集
 */
export function hasIntersection(arr1: any[], arr2: any[]) {
	if (!arr1 || !arr1.length) return false
  return arr1.some(item => arr2.includes(item))
}